{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import json\n",
    "import mimeparse\n",
    "import requests\n",
    "import urllib\n",
    "import pandas as pd\n",
    "from pprint import pprint as pp\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "MEETUP_API_HOST = 'https://api.meetup.com'\n",
    "EVENTS_URL = MEETUP_API_HOST + '/2/events.json'\n",
    "MEMBERS_URL = MEETUP_API_HOST + '/2/members.json'\n",
    "GROUPS_URL = MEETUP_API_HOST + '/2/groups.json'\n",
    "RSVPS_URL = MEETUP_API_HOST + '/2/rsvps.json'\n",
    "PHOTOS_URL = MEETUP_API_HOST + '/2/photos.json'\n",
    "GROUP_URLNAME = 'London-Machine-Learning-Meetup'\n",
    "\n",
    "# GROUP_URLNAME = 'Data-Science-London'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Load Meetup API Key\n",
    "meetup_api_key = pd.read_csv(\"../meetup_token.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class MeetupAPI(object):\n",
    "    \"\"\" Retreives information about meetup.com\n",
    "    \"\"\"\n",
    "    def __init__(self, api_key, num_past_events=10, http_timeout=1, http_retries=2):\n",
    "        \"\"\" Create new instance of meetup \"\"\"\n",
    "        self._api_key = api_key\n",
    "        self._http_timeout = http_timeout\n",
    "        self._http_retries = http_retries\n",
    "        self._num_past_events = num_past_events\n",
    "\n",
    "    \n",
    "    def get_past_events(self):\n",
    "        \"\"\" Get past meetup events for a given meetup group \"\"\"\n",
    "        params = {'key': self._api_key,\n",
    "                  'group_urlname': GROUP_URLNAME,\n",
    "                  'status': 'past',\n",
    "                  'desc': 'true'}\n",
    "        if self._num_past_events:\n",
    "            params['page'] = str(self._num_past_events)\n",
    "            \n",
    "        query = urllib.urlencode(params)\n",
    "        url = '{0}?{1}'.format(EVENTS_URL, query)\n",
    "        response = requests.get(url, timeout=self._http_timeout)\n",
    "        data = response.json()['results']\n",
    "        return data\n",
    "    \n",
    "    \n",
    "\n",
    "    def get_members(self):\n",
    "        \"\"\" Get meetup members for a given meetup group \"\"\"\n",
    "        params = {'key': self._api_key,\n",
    "                  'group_urlname': GROUP_URLNAME,\n",
    "                  'offset': '0',\n",
    "                  'format': 'json',\n",
    "                  'page': '100',\n",
    "                  'order':'name'}\n",
    "        query = urllib.urlencode(params)\n",
    "        url = '{0}?{1}'.format(MEMBERS_URL, query)\n",
    "        response = requests.get(url, timeout=self._http_timeout)\n",
    "        data = response.json()['results']\n",
    "        return data\n",
    "    \n",
    "    \n",
    "    def get_groups_by_member(self, member_id='38680722'):\n",
    "        \"\"\"Get meetup groups for a given meetup member \"\"\"\n",
    "        params = {'key': self._api_key,\n",
    "                  'member_id': member_id,\n",
    "                  'offset': '0',\n",
    "                  'format':'json',\n",
    "                  'page':'100',\n",
    "                  'order':'id'}\n",
    "        query = urllib.urlencode(params)\n",
    "        url = '{0}?{1}'.format(GROUPS_URL, query)\n",
    "        response = requests.get(url, timeout=self._http_timeout)\n",
    "        data = response.json()['results']\n",
    "        return data\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "m = MeetupAPI(api_key=meetup_api_key.values.flatten()[0])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{u'created': 1448462982000,\n",
      " u'description': u'<p>This month we welcome everybody back at the Barclays Escalator for a talk on Bayesian anomaly detection with John from BayesServer and Ed Challis of Re:Infer (Cyberhawk can no longer attend unfortunately).</p> <p>Agenda:</p> <p>7:00 - Pizza &amp; Networking</p> <p>7:30 - Bayesian Anomaly Detection</p> <p>8:15 - Pizza &amp; Networking</p> <p>8:30 - Re:Infer</p> <p>9:15 - Close</p> <p>This meetup is sponsored by the awesome\\xa0<a href=\"http://www.skimlinks.com\">Skimlinks</a>.</p> <p>See you there!</p> <p><br/>-- London ML Team</p> <p>\\n\\n\\n<b>\\u2022\\xa0</b><b>How to build an anomaly detection system with Bayesian networks - <a href=\"https://twitter.com/BayesServer\">John Sandiford\\xa0</a><br/></b></p> <p><br/>In this talk we demonstrate how to build a powerful yet simple anomaly detection system using Bayesian networks. The talk is accessible to all, with basic concepts explained, but also covers more advanced topics that are important in practice, such as initialization (e.g. with methods found in Deep Learning), detecting the number of clusters, missing data and numerical stability.\\xa0 The techniques discussed can be deployed on traditional systems or distributed platforms such as Apache Spark.</p> <p><b><i>Bio: </i></b>John is the CTO at Bayes Server, with 15 years\\u2019 experience as a machine learning developer and advisor. He is currently working with well-known clients such as BP commodity trading, and spent 8 years on an Artificial Intelligence research program for GE / USAF. He holds a PhD in Machine learning from Imperial.</p> <p>\\n\\n\\n\\u2022\\xa0<b>Deep learning for sentence embeddings and conversational interfaces - Ed Challis</b></p> <p><br/>Re:infer is building tools that allow developers to quickly create conversational interfaces. In this talk I\\'ll give an introduction to what we\\'re building and how we\\'ve built it. That includes the deep nets we\\'ve developed to learn distributed embeddings of sentences; how we use sentence-embeddings to facilitate fast learning and inference on supervised-learning problems; and our approach to human-in-the-loop active-learning.</p> <p><b><br/></b> <b>Bio:\\xa0</b>Ed is co-founder of re:infer. re:infer is an ambitious technology startup that\\'s bringing the latest advances in artificial intelligence and deep learning to create intelligent natural language interfaces that let companies automate knowledge worker tasks. Ed has 10 years experience working in the fields of machine learning and data science. He holds a PhD in Machine Learning from UCL and a Masters in AI from Edinburgh.</p>',\n",
      " u'duration': 9000000,\n",
      " u'event_url': u'http://www.meetup.com/London-Machine-Learning-Meetup/events/227013761/',\n",
      " u'group': {u'created': 1322826414000,\n",
      "            u'group_lat': 51.52000045776367,\n",
      "            u'group_lon': -0.18000000715255737,\n",
      "            u'id': 2894492,\n",
      "            u'join_mode': u'open',\n",
      "            u'name': u'London Machine Learning Meetup',\n",
      "            u'urlname': u'London-Machine-Learning-Meetup',\n",
      "            u'who': u'Machine Learning Enthusiasts'},\n",
      " u'headcount': 0,\n",
      " u'how_to_find_us': u'Note the entrance is through Foxcroft & Ginger.',\n",
      " u'id': u'227013761',\n",
      " u'maybe_rsvp_count': 0,\n",
      " u'name': u'Bayesian Anomaly Detection and Conversational Interfaces',\n",
      " u'rating': {u'average': 0, u'count': 0},\n",
      " u'rsvp_limit': 140,\n",
      " u'status': u'past',\n",
      " u'time': 1450465200000,\n",
      " u'updated': 1450475190000,\n",
      " u'utc_offset': 0,\n",
      " u'venue': {u'address_1': u'69-89 Mile End Road, E1 4TT',\n",
      "            u'city': u'London',\n",
      "            u'country': u'gb',\n",
      "            u'id': 19978922,\n",
      "            u'lat': 51.520901,\n",
      "            u'localized_country_name': u'United Kingdom',\n",
      "            u'lon': -0.052804,\n",
      "            u'name': u'The Innovation Loft',\n",
      "            u'repinned': False},\n",
      " u'visibility': u'public',\n",
      " u'waitlist_count': 90,\n",
      " u'yes_rsvp_count': 140}\n"
     ]
    }
   ],
   "source": [
    "last_meetups = m.get_past_events()\n",
    "pp(last_meetups[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Get information about the MeetUp members"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "ReadTimeout",
     "evalue": "HTTPSConnectionPool(host='api.meetup.com', port=443): Read timed out. (read timeout=1)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mReadTimeout\u001b[0m                               Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-9-2a59a87e49c4>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmembers\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mm\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_members\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0mmembers\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m<ipython-input-5-b9bc0286725c>\u001b[0m in \u001b[0;36mget_members\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m     37\u001b[0m         \u001b[0mquery\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0murllib\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0murlencode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mparams\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     38\u001b[0m         \u001b[0murl\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'{0}?{1}'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mMEMBERS_URL\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mquery\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 39\u001b[0;31m         \u001b[0mresponse\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrequests\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0murl\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_http_timeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     40\u001b[0m         \u001b[0mdata\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mresponse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjson\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'results'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     41\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/Users/RichardAfolabi/anaconda/envs/python27/lib/python2.7/site-packages/requests/api.pyc\u001b[0m in \u001b[0;36mget\u001b[0;34m(url, params, **kwargs)\u001b[0m\n\u001b[1;32m     65\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     66\u001b[0m     \u001b[0mkwargs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msetdefault\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'allow_redirects'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 67\u001b[0;31m     \u001b[0;32mreturn\u001b[0m \u001b[0mrequest\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'get'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0murl\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mparams\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mparams\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     68\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     69\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/Users/RichardAfolabi/anaconda/envs/python27/lib/python2.7/site-packages/requests/api.pyc\u001b[0m in \u001b[0;36mrequest\u001b[0;34m(method, url, **kwargs)\u001b[0m\n\u001b[1;32m     51\u001b[0m     \u001b[0;31m# cases, and look like a memory leak in others.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     52\u001b[0m     \u001b[0;32mwith\u001b[0m \u001b[0msessions\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mSession\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0msession\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 53\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0msession\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrequest\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmethod\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmethod\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0murl\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0murl\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     54\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     55\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/Users/RichardAfolabi/anaconda/envs/python27/lib/python2.7/site-packages/requests/sessions.pyc\u001b[0m in \u001b[0;36mrequest\u001b[0;34m(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)\u001b[0m\n\u001b[1;32m    466\u001b[0m         }\n\u001b[1;32m    467\u001b[0m         \u001b[0msend_kwargs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msettings\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 468\u001b[0;31m         \u001b[0mresp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprep\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0msend_kwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    469\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    470\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0mresp\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/Users/RichardAfolabi/anaconda/envs/python27/lib/python2.7/site-packages/requests/sessions.pyc\u001b[0m in \u001b[0;36msend\u001b[0;34m(self, request, **kwargs)\u001b[0m\n\u001b[1;32m    574\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    575\u001b[0m         \u001b[0;31m# Send the request\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 576\u001b[0;31m         \u001b[0mr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0madapter\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrequest\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    577\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    578\u001b[0m         \u001b[0;31m# Total elapsed time of the request (approximately)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/Users/RichardAfolabi/anaconda/envs/python27/lib/python2.7/site-packages/requests/adapters.pyc\u001b[0m in \u001b[0;36msend\u001b[0;34m(self, request, stream, timeout, verify, cert, proxies)\u001b[0m\n\u001b[1;32m    447\u001b[0m                 \u001b[0;32mraise\u001b[0m \u001b[0mSSLError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrequest\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mrequest\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    448\u001b[0m             \u001b[0;32melif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mReadTimeoutError\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 449\u001b[0;31m                 \u001b[0;32mraise\u001b[0m \u001b[0mReadTimeout\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrequest\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mrequest\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    450\u001b[0m             \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    451\u001b[0m                 \u001b[0;32mraise\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mReadTimeout\u001b[0m: HTTPSConnectionPool(host='api.meetup.com', port=443): Read timed out. (read timeout=1)"
     ]
    }
   ],
   "source": [
    "members = m.get_members()\n",
    "members"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
